/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @author Simon Oliver
 * @version 1.0
 */
 
import wilberforce.ui.canvas.AbstractCanvasItem;
import wilberforce.geom.rect;
import wilberforce.ui.canvas.transformableObjectModifiedEvent;

// Classes from pixlib
import com.bourre.data.libs.GraphicLib;
import com.bourre.commands.Delegate;
import com.bourre.data.libs.ILibListener;
import com.bourre.data.libs.LibEvent;

class wilberforce_demos.canvas.canvasDemo1ImageItem extends AbstractCanvasItem implements ILibListener
{
	var _imageUrl:String;
	var _imageLoader:GraphicLib;
	var _image:MovieClip;
	
	var _loaded:Boolean;
	
	function canvasDemo1ImageItem(url:String)
	{
		_width=150;
		_height=150;
		_imageUrl=url;
		_loaded=false;
	}
	
	public function createVisualElement(container:MovieClip,x:Number,y:Number)
	{
		_x=x;
		_y=y;
		_itemContainer=container;
		_itemContainer._x=x;
		_itemContainer._y=y;
		
		// This is where we would load in the image item into the container
		//_itemContainer.loadMovie(_imageUrl);
		_imageLoader=new GraphicLib(_itemContainer,0,true);
		_imageLoader.load(_imageUrl);		
		_imageLoader.addListener(this);
		//GraphicLib.
	}
	
	public function onLoadInit(e:LibEvent):Void
	{
		trace("Image loaded");
		
		_image=_imageLoader.getView();
		trace("Image is "+_image);
		_width=_image._width;
		_height=_image._height;
		trace("SIZE "+_width+","+_height);
		_activeRect=new rect(_x,_y,_x+_width,_y+_height);
		_transformableObject.updateShape(_activeRect);
		
		_loaded=true;
	}
	
	public function onTimeOut(e:LibEvent):Void
	{
		trace("Image timeout");
	}
	
	public function onLoadProgress(e:LibEvent):Void
	{
		trace("Image progress"+e.getPerCent());
	}
	
	public function onTransformableObjectNewDimensions(e : transformableObjectModifiedEvent)
	{
		var tRect:rect=rect(e.shape);
		
		_x=tRect.x;
		_y=tRect.y;
		
		_width=tRect.width;
		_height=tRect.height;
		
		_itemContainer._x=x;
		_itemContainer._y=y;
		
		if (_loaded)
		{
			_itemContainer._width=_width;
			_itemContainer._height=_height;
		}
		
		/*if (e.isFinal)
		{
			_textSystem.clear();
			_textSystem.render(true);
		}
		*/
	}
}